<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$message_id = $route->getAdminId();
$this_user = "recipient"; // получатель

$message = $db->getRow("select `M`.*,`U`.`name` as `sender_name`,`U`.`sname` as `sender_sname` from `messages` as `M`,`users` as `U` where `M`.`message_id`='$message_id' and `M`.`recipient_id` = '".$user->getUserId()."' and `M`.`sender_id` = `U`.`user_id` limit 1");
if (!$message)
{
	$message = $db->getRow("select `M`.*,`U`.`name` as `sender_name`,`U`.`sname` as `sender_sname` from `messages` as `M`,`users` as `U` where `M`.`message_id`='$message_id' and `M`.`sender_id` = '".$user->getUserId()."' and `M`.`recipient_id` = `U`.`user_id` limit 1");
	if (!$message)
		goToPage("/coordinator/messages");
	$this_user = "sender"; // отправитель
}

if ($this_user == "recipient" && $message['read'] == 0) // если получатель и не прочитано, делаем прочитанным
	$db->sendQuery("update `messages` set `read` = '1' where `message_id`='$message_id' and `recipient_id` = '".$user->getUserId()."' limit 1");

require_once COORDINATOR.DS."head.php";
?>

<div class="submenu_container">
	<a class="submenu_button" href='/coordinator/messages/received'>Полученные</a>
	<a class="submenu_button" href='/coordinator/messages/sended'>Отправленные</a>
	<a class="submenu_button" href='/coordinator/messages/send'>Отправить</a>
	<a class="submenu_button submenu_button_active">Сообщение</a>
</div>
<h3>Сообщение</h3>
<form method='post' action='/coordinator/messages/send'>
	<input type='hidden' name='act' value='post' />
	<input type='hidden' name='whom' value='<?php echo $this_user == "recipient" ? $message['sender_id'] : $message['recipient_id']; ?>' />
	<input type='hidden' name='header' value='Re:<?php echo $message['header']; ?>' />
	<table>
		<tr>
			<td><?php echo $this_user == "recipient" ? "Отправитель:" : "Получатель:";?></td>
			<td><p><?php echo $message['sender_name']." ".$message['sender_sname']; ?></p></td>
		</tr>
		<tr valign='top'>
			<td>Тема:</td>
			<td><p><?php echo $message['header']; ?></p></td>
		</tr>
		<tr valign='top'>
			<td>Текст:</td>
			<td><p><?php echo str_replace("\n","</p><p>",$message['text']); ?></p></td>
		</tr>
		<tr valign='top'>
			<td><?php echo $this_user == "recipient" ? "Текст ответа:" : "Текст сообщения:";?></td>
			<td><textarea rows='5' style="width:300px" name='text'></textarea></td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Ответить' /></td>
		</tr>
	</table>
</form>

<?php require_once COORDINATOR.DS."footer.php"; ?>
